home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / music_utilities / pt030.dms / pt030.adf / Less / Src / output.c < prev    next >
C/C++ Source or Header  |  1987-06-15  |  4KB  |  250 lines

  1. /*
  2.  * High level routines dealing with the output to the screen.
  3.  */
  4.  
  5. #include "less.h"
  6.  
  7. public int errmsgs;    /* Count of messages displayed by error() */
  8.  
  9. extern int sigs;
  10. extern int sc_width, sc_height;
  11. extern int ul_width, ue_width;
  12. extern int so_width, se_width;
  13. extern int bo_width, be_width;
  14. extern int tabstop;
  15. extern int twiddle;
  16. extern int any_display;
  17. extern char *line;
  18. extern char *first_cmd;
  19.  
  20. /*
  21.  * Display the line which is in the line buffer.
  22.  */
  23.     public void
  24. put_line()
  25. {
  26. #ifdef AMIGA
  27.     register unsigned char *p;
  28. #else
  29.     register char *p;
  30. #endif
  31.     register int c;
  32.     register int column;
  33.     extern int auto_wrap, ignaw;
  34.  
  35.     if (sigs)
  36.         /*
  37.          * Don't output if a signal is pending.
  38.          */
  39.         return;
  40.  
  41.     if (line == NULL)
  42.         line = (twiddle) ? "~" : "";
  43.  
  44.     column = 0;
  45. #ifdef AMIGA
  46.     for (p = (unsigned char *)line;  *p != '\0';  p++)
  47. #else
  48.     for (p = line;  *p != '\0';  p++)
  49. #endif
  50.     {
  51.         switch (c = *p)
  52.         {
  53.         case UL_CHAR:
  54.             ul_enter();
  55.             column += ul_width;
  56.             break;
  57.         case UE_CHAR:
  58.             ul_exit();
  59.             column += ue_width;
  60.             break;
  61.         case BO_CHAR:
  62.             bo_enter();
  63.             column += bo_width;
  64.             break;
  65.         case BE_CHAR:
  66.             bo_exit();
  67.             column += be_width;
  68.             break;
  69.         case '\t':
  70.             do
  71.             {
  72.                 putchr(' ');
  73.                 column++;
  74.             } while ((column % tabstop) != 0);
  75.             break;
  76.         case '\b':
  77.             putbs();
  78.             column--;
  79.             break;
  80.         default:
  81.             if (c & 0200)
  82.             {
  83.                 /*
  84.                  * Control characters arrive here as the
  85.                  * normal character [carat_char(c)] with
  86.                  * the 0200 bit set.  See pappend().
  87.                  */
  88.                 putchr('^');
  89.                 putchr(c & 0177);
  90.                 column += 2;
  91.             } else
  92.             {
  93.                 putchr(c);
  94.                 column++;
  95.             }
  96.         }
  97.     }
  98.     if (column < sc_width || !auto_wrap || ignaw)
  99.         putchr('\n');
  100. }
  101.  
  102. /*
  103.  * Is a given character a "control" character?
  104.  * {{ ASCII DEPENDENT }}
  105.  */
  106.     public int
  107. control_char(c)
  108.     int c;
  109. {
  110.     return (c < ' ' || c == '\177');
  111. }
  112.  
  113. /*
  114.  * Return the printable character used to identify a control character
  115.  * (printed after a carat; e.g. '\3' => "^C").
  116.  * {{ ASCII DEPENDENT }}
  117.  */
  118.     public int
  119. carat_char(c)
  120.     int c;
  121. {
  122.     return ((c == '\177') ? '?' : (c | 0100));
  123. }
  124.  
  125.  
  126. static char obuf[1024];
  127. static char *ob = obuf;
  128.  
  129. /*
  130.  * Flush buffered output.
  131.  */
  132.     public void
  133. flush()
  134. {
  135. #ifdef AMIGA
  136.     extern struct FileHandle *tty;
  137.     extern nrow;
  138.  
  139.     if (nrow == 0)
  140.         ttopen();
  141.  
  142.     ttflush();
  143.     Write(tty, obuf, (long) ob-obuf);
  144. #else
  145.     write(1, obuf, ob-obuf);
  146. #endif
  147.     ob = obuf;
  148. }
  149.  
  150. /*
  151.  * Discard buffered output.
  152.  */
  153.     public void
  154. dropout()
  155. {
  156.     ob = obuf;
  157. }
  158.  
  159. /*
  160.  * Output a character.
  161.  */
  162.     public void
  163. putchr(c)
  164.     int c;
  165. {
  166.     if (ob >= &obuf[sizeof(obuf)])
  167.         flush();
  168.     *ob++ = c;
  169. }
  170.  
  171. /*
  172.  * Output a string.
  173.  */
  174.     public void
  175. putstr(s)
  176.     register char *s;
  177. {
  178.     while (*s != '\0')
  179.         putchr(*s++);
  180. }
  181.  
  182. /*
  183.  * Output a message in the lower left corner of the screen
  184.  * and wait for carriage return.
  185.  */
  186.  
  187. static char return_to_continue[] = "  (press RETURN)";
  188.  
  189.     public void
  190. error(s)
  191.     char *s;
  192. {
  193.     register int c;
  194.     static char buf[2];
  195.  
  196.     errmsgs++;
  197.     if (!any_display)
  198.     {
  199.         /*
  200.          * Nothing has been displayed yet.
  201.          * Output this message on error output (file
  202.          * descriptor 2) and don't wait for a keystroke
  203.          * to continue.
  204.          *
  205.          * This has the desirable effect of producing all
  206.          * error messages on error output if standard output
  207.          * is directed to a file.  It also does the same if
  208.          * we never produce any real output; for example, if
  209.          * the input file(s) cannot be opened.  If we do
  210.          * eventually produce output, code in edit() makes
  211.          * sure these messages can be seen before they are
  212.          * overwritten or scrolled away.
  213.          */
  214.         write(2, s, strlen(s));
  215.         write(2, "\n", 1);
  216.         return;
  217.     }
  218.  
  219.     lower_left();
  220.     clear_eol();
  221.     so_enter();
  222.     putstr(s);
  223.     putstr(return_to_continue);
  224.     so_exit();
  225.  
  226. #if ONLY_RETURN
  227.     while ((c = getchr()) != '\n' && c != '\r')
  228.         bell();
  229. #else
  230.     c = getchr();
  231.     if (c != '\n' && c != '\r' && c != ' ')
  232.     {
  233.         buf[0] = c;
  234.         first_cmd = buf;
  235.     }
  236. #endif
  237.     lower_left();
  238.  
  239.     if (strlen(s) + sizeof(return_to_continue) + 
  240.         so_width + se_width + 1 > sc_width)
  241.         /*
  242.          * Printing the message has probably scrolled the screen.
  243.          * {{ Unless the terminal doesn't have auto margins,
  244.          *    in which case we just hammered on the right margin. }}
  245.          */
  246.         repaint();
  247.  
  248.     flush();
  249. }
  250.